layui.use(['layer', 'form'], function() {

    var layer = layui.layer
        ,form = layui.form;

    $(function() {
        form.on('select(Select)',function (data) {
            if (data.value == '1') {
                $(".singleSelection").removeAttr("hidden");
                $(".multipleSelection").attr("hidden","hidden");
                $(".choices").removeAttr("hidden");
            } else if (data.value == '2') {
                $(".singleSelection").attr("hidden","hidden");
                $(".multipleSelection").removeAttr("hidden");
                $(".choices").removeAttr("hidden");
            } else if (data.value == '3') {
                $(".choices").attr("hidden","hidden");
            }
        });

        form.on('submit(formDemo)', function(data) {
            var uId = data.field.uId
                ,qTitle = data.field.qTitle
                ,qType = data.field.qType
                ,qAnswer1 = data.field.qAnswer1
                ,qAnswer2 = data.field.qAnswer2
                ,qAnswer3 = data.field.qAnswer3
                ,qAnswer4 = data.field.qAnswer4
                ,qResult
                ,qAnalyze = data.field.qAnalyze==""?null:data.field.qAnalyze
                ,qScore = data.field.qScore
                ,reg = /^[0-9]\d*$/;

            if (qType == '1'){   //单选答案
                qResult = $(".singleResult:checked").val();
            } else if (qType == '2'){  //多选答案
                var array = new Array();
                $(".multipleResult[name='multipleResult']:checked").each(function () {
                    array.push($(this).val());
                });
                qResult = array.join(",");
            } else if (qType == '3'){
                qAnswer1 = null;
                qAnswer2 = null;
                qAnswer3 = null;
                qAnswer4 = null;
                qResult = null;
            }

            if (data.field.qType != '3') {
                if (qAnswer1 == null || qAnswer1 == ''){
                    layer.msg("请输入选项A的内容", {"icon": 0, "time": 1000});
                    $(".qAnswer1").focus();
                    return false;
                }
                if (qAnswer2 == null || qAnswer2 == ''){
                    layer.msg("请输入选项B的内容", {"icon": 0, "time": 1000});
                    $(".qAnswer2").focus();
                    return false;
                }
                if (qAnswer3 == null || qAnswer3 == ''){
                    layer.msg("请输入选项C的内容", {"icon": 0, "time": 1000});
                    $(".qAnswer3").focus();
                    return false;
                }
                if (qAnswer4 == null || qAnswer4 == ''){
                    layer.msg("请输入选项D的内容", {"icon": 0, "time": 1000});
                    $(".qAnswer4").focus();
                    return false;
                }
                if (qResult == null || qResult == ''){
                    layer.msg("请选择正确答案！", {"icon": 0, "time": 1000});
                    return false;
                }
            }

            if (!reg.test(qScore)){
                $(".exist_msg").html("请输入正确的数字").show();
                return false;
            } else {
                $(".exist_msg").hide();
            }

            $.ajax({
                url:"/question/insert",
                type:"post",
                data:{
                    uId:uId,
                    qTitle:qTitle,
                    qType:qType,
                    qAnswer1:qAnswer1,
                    qAnswer2:qAnswer2,
                    qAnswer3:qAnswer3,
                    qAnswer4:qAnswer4,
                    qResult:qResult,
                    qAnalyze:qAnalyze,
                    qScore:qScore
                },
                success:function (result) {
                    if (result.code == 200){
                        //题目添加成功
                        layer.msg(result.msg);
                        setTimeout(function () {
                            window.location.href="/questionList";
                        }, 1000);
                    } else {
                        layer.msg(result.msg);
                    }
                }
            });
            return false;
        })
    })
});